package top.wugy.blog.routes;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import top.wugy.blog.kits.BlogKit;
import top.wugy.blog.kits.Const;
import top.wugy.blog.model.TLink;
import top.wugy.blog.model.TTimeLine;
import top.wugy.blog.web.AuthInterceptor;

/**
 * wugy on 2017/11/13 13:16
 */
public class AssistController extends BaseController {

    @Before(AuthInterceptor.class)
    public void saveLink() {
        TLink link = getModel(TLink.class, "link");
        if (null == link.getId()) {
            link.save();
        } else {
            link.update();
        }
        renderOk();
    }

    @Before(AuthInterceptor.class)
    public void saveTimeLine() {
        TTimeLine timeLine = getModel(TTimeLine.class, "timeline");
        if (null == timeLine.getId()) {
            timeLine.save();
        } else {
            timeLine.update();
        }
        renderOk();
    }

    public void timeline() {
        Page<Record> recordPage = TTimeLine.dao.timeline(getPageNum());
        recordPage.getList().forEach(timeline -> timeline.set("createTime",
                BlogKit.date2Str(timeline.getDate("createTime"), Const.DEF_DATE_FORMAT)));
        renderOk(recordPage);
    }
}
